package com.dl.utils;

import com.dl.config.shiro.UserRealm;
import com.dl.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

/**
 * shiro常用操作工具类
 * @author DL
 * @create 2021-06-03 15:45
 */
@Component
public class ShiroUtils {

    public static UserRealm getRealm(){
        //获取安全管理器
        RealmSecurityManager securityManager = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        //通过安全管理器获取realm
        return  (UserRealm) securityManager.getRealms().iterator().next();
    }

    /**
     * 获得当前用户信息
     * @return
     */
    public static User getUser() {
        return (User) SecurityUtils.getSubject().getSession().getAttribute(ConstantField.LOGIN_INFO);
    }

    /**
     * 获得当前用户sessionId
     * @return
     */
    public static String getSessionId() {
        return (String) SecurityUtils.getSubject().getSession().getId();
    }

    /**
     * 切换角色
     */
    public void switchRole(){
/*        String realmName = subject.getPrincipals().getRealmNames().iterator().next();
        SimplePrincipalCollection principals = new SimplePrincipalCollection(username, realmName);
        subject.runAs(principals);
        //用realm获取授权缓存，并通过username删除该缓存
        Cache<Object, AuthorizationInfo> authorizationCache = realm.getAuthorizationCache();
        authorizationCache.remove(username);

        //切换身份也就是刷新了
        subject.releaseRunAs();*/
    }

    /**
     * 清空当前用户授权缓存
     * @return
     */
    public static boolean clearAuthorizationCache() {
        return clearAuthorizationCache(null);
    }

    /**
     * 清空指定用户授权缓存
     * 删除缓存中对应用户的授权信息，下一次直接访问数据库
     * @return
     */
    public static boolean clearAuthorizationCache(String username) {
        boolean flag = true;
        try {
            UserRealm realm = getRealm();
            //获取用户主体
            Subject subject = SecurityUtils.getSubject();
            //获取用户名
            if (username == null){
                //获取当前用户名
                username = (String) subject.getPrincipal();
            }
            Cache<Object, AuthorizationInfo> authorizationCache = realm.getAuthorizationCache();
            authorizationCache.remove(username);
        } catch (NullPointerException | IllegalStateException | CacheException e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }

    /**
     * 清空当前用户认证缓存
     * @return
     */
    public static boolean clearAuthenticationCache() {
        return clearAuthenticationCache(null);
    }

    /**
     * 清空指定认证缓存
     * @return
     */
    public static boolean clearAuthenticationCache(String username) {
        boolean flag = true;
        try {
            UserRealm realm = getRealm();
            //获取用户主体
            Subject subject = SecurityUtils.getSubject();
            if (username == null){
                //获取当前用户名
                username = (String) subject.getPrincipal();
            }
            Cache<Object, AuthenticationInfo> authenticationCache = realm.getAuthenticationCache();
            authenticationCache.remove(username);
        } catch (NullPointerException | IllegalStateException | CacheException e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }
}
